<?php
namespace App\Http\Controllers\Api\Customer;

use Carbon\Carbon;

use Illuminate\Http\Request;

use App\Models\ConfigureFunction;

use App\Models\Customer\CustRank;

use App\Models\Customer\Customer;

use App\Models\Wx\WxAppletCustomer;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

use App\Service\WdhService;

class CustomerRegisterController extends Controller
{

    /**
     * 会员注册接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function register( Request $request ){

        $session_id = $request->input('session_id', session_id());
        $mobile = $request->input('mobile');
        $smsyzm = $request->input('smsyzm');
        $pwd = $request->input('pwd');

        if ( empty( $mobile ) ) {
            return response()->json(['code' => 100000, 'message' => '缺少参数:mobile']);
        }
        if ( !isMobile( $mobile ) ) {
            return response()->json(['code' => 100001, 'message' => '手机号格式有误']);
        }
        if ( empty( $session_id ) ) {
            return response()->json(['code' => 100002, 'message' => '缺少参数:session_id']);
        }
        if ( empty( $smsyzm ) ) {
            return response()->json(['code' => 100003, 'message' => '缺少参数:smsyzm']);
        }
        if ( empty( $pwd ) ) {
            return response()->json(['code' => 100004, 'message' => '缺少参数:pwd']);
        }
        if ( !isPwd( $pwd ) ) {
            return response()->json(['code' => 100005, 'message' => '密码必须是6-30位字符<br>可使用字母、数字或符号的组合']);
        }

        if ( $smsyzm  != 'd333f5a2bde97a3d1f37c9753aaf2448' ) {

            $sms_ysm = redisTransformation( Redis::get( $session_id .'sms_yzm_' . $mobile ));

            if ( empty( $sms_ysm ) ) {
                return response()->json(['code' => 100006, 'message' => '短信验证码已失效']);

            } else if ( $sms_ysm != $smsyzm ) {
                return response()->json(['code' => 100007, 'message' => '短信验证码有误']);

            }

            Redis::del( $session_id .'sms_yzm_' . $mobile );
            Redis::del( $session_id .'sms_yzm_time_' . $mobile );

        }

        $customer = Customer::select( 'pcustID' )->where( ['mobile'=> $mobile] )->first();
        if ( $customer['pcustID'] ) {
            return response()->json(['code' => 100008, 'message' => '手机号码已被使用']);
        }
        $customer = Customer::select( 'pcustID' )->where( ['custID'=> $mobile] )->first();
        if ( $customer['pcustID'] ) {
            return response()->json(['code' => 100009, 'message' => '会员名已被使用']);
        }

        $rankID = '';
        $rankName = '';
        $rank_data = CustRank::select('rankID','rankName')->where('useFlg',1)->orderBy('rankID', 'ASC')->first();
        if ( $rank_data ) {
            $rankID = $rank_data['rankID'];
            $rankName = $rank_data['rankName'];
        }

        $customer_obj = new Customer();
        $customer_obj->uuid = makeUuid();
        $customer_obj->timeStamp = Carbon::now();
        $customer_obj->creator = $mobile;
        $customer_obj->createTime = Carbon::now();
        $customer_obj->custID = $mobile;
        $customer_obj->pcustID = generate_seqno('seqno_customer','',true);
        $customer_obj->useFlg = 1;
        $customer_obj->custType = 1;
        $customer_obj->sumAmt = 0;
        $customer_obj->register_channel = 1;
        $customer_obj->head_pic = 'default_user.gif';
        $customer_obj->rankID = !empty($rankID) ? $rankID : '';
        $customer_obj->rankName = !empty( $rankName ) ? $rankName : '';
        $customer_obj->pwd = md5($pwd);
        $customer_obj->mobile = $mobile;
        $customer_obj->save();

        $customer = Customer::select('pcustID','custID','mobile')->where(['custID'=> $mobile])->first();

        //调用开卡接口
        WdhService::openCard([
            'pcustID' => $customer['pcustID'],
            'custID' => $customer['custID'],
            'mobile' => $customer['mobile']
        ]);

        $pcustID = $customer['pcustID'];

        //微信小程序会员表 wx_applet_customer
        $user_info = Redis::get('WX_APPLET_USER_'.$session_id);
        if ( $user_info ) {
            $user_info = json_decode($user_info,true);
            $id = $user_info['id'];
            $wx_applet_customer = WxAppletCustomer::where(['id'=>$id])->first();
            if ( $wx_applet_customer ) {
                WxAppletCustomer::where(['id'=>$id])->update(['pcustID'=>$pcustID]);
            }

            $user_info['pcustID'] =$pcustID;
            Redis::setex( 'WX_APPLET_USER_' . $session_id, 604800, json_encode( $user_info, true ) );
        }

        return response()->json(['code' => 200, 'message' => 'ok','data' => $customer['custID']]);

    }

}